package wycenanieruchomosci;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.File;



public class CsvReader {

    private FileChooserFrame fileChooserFrame;
    private List<String> argumentsList = new ArrayList<String>();
    private Map<Integer, List<Integer>> valueMap;

    public CsvReader(File file, FileChooserFrame fileChooserFrame) {
        this.fileChooserFrame = fileChooserFrame;
        argumentsList = new ArrayList<String>();
        valueMap = new HashMap<Integer, List<Integer>>();
        readCsvFile(file);
    }

    private void readCsvFile(File file) {
        
        if(file.getName().contains(".xls")){
            XLS2CSV xls2csv = new XLS2CSV();
            
        }
        String dataLine;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
            String readLine = reader.readLine();
            String[] spleatedLine = readLine.split(";");
            for (String arguments : spleatedLine) {
                int counter = 0;
                if (arguments != null && !arguments.isEmpty() && arguments.trim() != null) {
                    getArgumentsList().add(arguments.trim());
                }
            }
            while ((dataLine = reader.readLine()) != null) {
                List<Integer> argumentsList = new ArrayList<Integer>();
                Integer mapKey = 0;
                String[] spleatedDataLine = dataLine.split(";");
                boolean isFirstRow = true;
                for (String data : spleatedDataLine) {
                    if (data != null && !data.trim().isEmpty()) {
                        if (isFirstRow) {
                            mapKey = Integer.parseInt(data.trim());
                            isFirstRow = false;
                        } else {
                            argumentsList.add(Integer.parseInt(data.trim()));
                        }
                    }
                }
                getValueMap().put(mapKey, argumentsList);
            }

            fileChooserFrame.writeLinieOnConsole("Sprawdź poprawność danych: \n");
            fileChooserFrame.writeLinieOnConsole("Znaleziono Następujące argumenty: \n");
            fileChooserFrame.writeLinieOnConsole(getArgumentsList().toString()+ "\n");
            fileChooserFrame.writeLinieOnConsole("  Oraz "+ getValueMap().keySet().size() +" następujących wartości \n" );
            for (Integer i:getValueMap().keySet()){
                fileChooserFrame.writeLinieOnConsole("dla " + i+ " argumenty " + getValueMap().get(i).toString() + "\n");
            }
     
        } catch (IOException ex) {
            Logger.getLogger(FileChooserFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<String> getArgumentsList() {
        return argumentsList;
    }

    public Map<Integer, List<Integer>> getValueMap() {
        return valueMap;
    }
}